package com.yunfeisoft.business.controller;

import com.yunfeisoft.business.model.Elder;
import com.yunfeisoft.business.model.PerceptionCommunicationEvaluation;
import com.yunfeisoft.business.service.inter.ElderService;
import com.yunfeisoft.business.service.inter.PerceptionCommunicationEvaluationService;
import com.yunfeisoft.common.base.BaseController;
import com.yunfeisoft.common.utils.Page;
import com.yunfeisoft.common.utils.Response;
import com.yunfeisoft.common.utils.ResponseUtils;
import com.yunfeisoft.common.utils.Validator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: PerceptionCommunicationEvaluationController
 * Description: 感知觉与沟通能力评估表Controller
 * Author: Jackie liu
 * Date: 2020-05-22
 */
@Controller
public class PerceptionCommunicationEvaluationController extends BaseController {

    @Autowired
    private PerceptionCommunicationEvaluationService perceptionCommunicationEvaluationService;

    @Autowired
    private ElderService elderService;

    /**
     * 添加感知觉与沟通能力评估表
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/perceptionCommunicationEvaluation/save", method = RequestMethod.POST)
    @ResponseBody
    public Response save(PerceptionCommunicationEvaluation record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "elderId", "姓名为空");
        validator.required(request, "consciousnessScore", "意识水平为空");
        validator.required(request, "visionScore", "视力为空");
        validator.required(request, "hearingScore", "听力为空");
        validator.required(request, "communicationScore", "沟通交流为空");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }
        // 计算等级
        // 获取意识水平
        Integer consciousnessScore = record.getConsciousnessScore();
        // 获取听力
        Integer hearingScore = record.getHearingScore();
        // 获取视力
        Integer visionScore = record.getVisionScore();
        // 获取沟通交流
        Integer communicationScore = record.getCommunicationScore();
        if (consciousnessScore == 0 && (hearingScore == 0 || hearingScore == 1)
            && (visionScore == 0 || visionScore == 1) && communicationScore == 0) {
            // 能力完好
            record.setLevel(0);
        } else if (consciousnessScore == 0 && (hearingScore == 2 || visionScore == 2 || communicationScore == 1)) {
            // 轻度受损
            record.setLevel(1);
        } else if ((consciousnessScore == 0 && (hearingScore == 3 || visionScore == 3 || communicationScore == 2))
            || (consciousnessScore == 1 && (hearingScore >= 3 || visionScore >= 3) && communicationScore >= 2)) {
            // 中度受损
            record.setLevel(2);
        } else if ((consciousnessScore == 0 || consciousnessScore == 1) && (hearingScore == 4 || visionScore == 4 || communicationScore == 3)
            || (consciousnessScore == 2 || consciousnessScore == 3)) {
            // 重度受损
            record.setLevel(3);
        }
        perceptionCommunicationEvaluationService.save(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 修改感知觉与沟通能力评估表
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/perceptionCommunicationEvaluation/modify", method = RequestMethod.POST)
    @ResponseBody
    public Response modify(PerceptionCommunicationEvaluation record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "id", "参数错误");
        validator.required(request, "elderId", "姓名为空");
        validator.required(request, "consciousnessScore", "意识水平为空");
        validator.required(request, "visionScore", "视力为空");
        validator.required(request, "hearingScore", "听力为空");
        validator.required(request, "communicationScore", "沟通交流为空");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }
        // 计算等级
        // 获取意识水平
        Integer consciousnessScore = record.getConsciousnessScore();
        // 获取听力
        Integer hearingScore = record.getHearingScore();
        // 获取视力
        Integer visionScore = record.getVisionScore();
        // 获取沟通交流
        Integer communicationScore = record.getCommunicationScore();
        if (consciousnessScore == 0 && (hearingScore == 0 || hearingScore == 1)
                && (visionScore == 0 || visionScore == 1) && communicationScore == 0) {
            // 能力完好
            record.setLevel(0);
        } else if (consciousnessScore == 0 && (hearingScore == 2 || visionScore == 2 || communicationScore == 1)) {
            // 轻度受损
            record.setLevel(1);
        } else if ((consciousnessScore == 0 && (hearingScore == 3 || visionScore == 3 || communicationScore == 2))
                || (consciousnessScore == 1 && (hearingScore >= 3 || visionScore >= 3) && communicationScore >= 2)) {
            // 中度受损
            record.setLevel(2);
        } else if ((consciousnessScore == 0 || consciousnessScore == 1) && (hearingScore == 4 || visionScore == 4 || communicationScore == 3)
                || (consciousnessScore == 2 || consciousnessScore == 3)) {
            // 重度受损
            record.setLevel(3);
        }
        perceptionCommunicationEvaluationService.modify(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 查询感知觉与沟通能力评估表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/perceptionCommunicationEvaluation/query", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response query(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        PerceptionCommunicationEvaluation record = perceptionCommunicationEvaluationService.load(id);
        if (!ObjectUtils.isEmpty(record)) {
            Elder elder = elderService.load(record.getElderId());
            record.setElderName(elder.getName());
        }
        return ResponseUtils.success(record);
    }

    /**
     * 分页查询感知觉与沟通能力评估表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/perceptionCommunicationEvaluation/list", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response list(HttpServletRequest request, HttpServletResponse response) {
        String name = ServletRequestUtils.getStringParameter(request, "name", null);
        Map<String, Object> params = new HashMap<String, Object>();
        initParams(params, request);
        if (!StringUtils.isBlank(name)) {
            params.put("elderName", name);
        }
        Page<PerceptionCommunicationEvaluation> page = perceptionCommunicationEvaluationService.queryPage(params);
        return ResponseUtils.success(page);
    }

    /**
     * 批量删除感知觉与沟通能力评估表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/perceptionCommunicationEvaluation/delete", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response delete(HttpServletRequest request, HttpServletResponse response) {
        String ids = ServletRequestUtils.getStringParameter(request, "ids", null);
        if (StringUtils.isBlank(ids)) {
            return ResponseUtils.warn("参数错误");
        }
        perceptionCommunicationEvaluationService.remove(ids.split(","));
        return ResponseUtils.success("删除成功");
    }
}
